package com.swy.shopgroup.network;


import android.content.Context;

import com.swy.shopgroup.entity.Acts;
import com.swy.shopgroup.entity.Goods;
import com.swy.shopgroup.entity.OutPage;
import com.swy.shopgroup.entity.ShopInfo;
import com.swy.shopgroup.entity.ShopUser;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttp {
    private OkHttpClient client;
    public static OkHttpClient appLoginClient;
    private Context context;

    public OkHttp() {
        client = new OkHttpClient.Builder()
                .connectTimeout(0, TimeUnit.SECONDS)
                .readTimeout(0, TimeUnit.SECONDS)
                .writeTimeout(0, TimeUnit.SECONDS)
                .build();
    }

    public Response getData(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url).addHeader("Accept", "application/json")
                .build();
        return client.newCall(request).execute();
    }


    public Response getDataWhitId(String url, String id) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .addFormDataPart("id", id)
                .setType(MultipartBody.FORM)
                .build();
        Request request = new Request.Builder()
                .url(url)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }

    public String getDataWithOutHead(String url) throws IOException {
        try {
            if (!url.contains("http://")) {
                url += "http://";
            }
            Request request = new Request.Builder()
                    .url(url)
                    .build();
            return client.newCall(request).execute().body().string();
        } catch (Exception e) {
            return url + "\n" + e.toString() + "!";
        }
    }

    public Response login(String name, String pass) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .addFormDataPart("name", name)
                .addFormDataPart("pwd", pass)
                .setType(MultipartBody.FORM)
                .build();
        Request request = new Request.Builder()
                .url(NetAdress.login)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }

    public Response register(File file, ShopUser user) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file))
                .addFormDataPart("name", user.getData().getName())
                .addFormDataPart("password", user.getData().getPassword())
                .addFormDataPart("nickname", user.getData().getNickname())
                .addFormDataPart("sex", user.getData().getSex())
                .addFormDataPart("type", user.getData().getType() + "")
                .addFormDataPart("avatar", "")
                .setType(MultipartBody.FORM)
                .build();
        Request request = new Request.Builder()
                .url(NetAdress.register)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }

    public Response shopRegister(File file, File file1, ShopInfo info) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file))
                .addFormDataPart("file1", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file1))
                .addFormDataPart("name", info.getData().getName())
                .addFormDataPart("shopinfo", info.getData().getShopinfo())
                .addFormDataPart("pagecode", info.getData().getPagecode())
                .addFormDataPart("state", info.getData().getState() + "")
                .addFormDataPart("userid", info.getData().getUserid() + "")
                .setType(MultipartBody.FORM)
                .build();
        Request request = new Request.Builder()
                .url(NetAdress.shopCommit)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }

    public Response shopSelectByUser(String id) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .addFormDataPart("id", id)
                .setType(MultipartBody.FORM)
                .build();
        Request request = new Request.Builder()
                .url(NetAdress.shopByUser)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }

    public Response insertGoods(File file, Goods.DataBean bean) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file))
                .addFormDataPart("name", bean.getName())
                .addFormDataPart("info", bean.getInfo())
                .addFormDataPart("price", bean.getPrice())
                .addFormDataPart("shopId", bean.getShopId() + "")
                .setType(MultipartBody.FORM)
                .build();
        Request request = new Request.Builder()
                .url(NetAdress.insertGoods)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }

    public Response insertAct(File file, Acts.DataBean bean) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .addFormDataPart("file", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file))
                .addFormDataPart("name", bean.getName())
                .addFormDataPart("content", bean.getContent())
                .addFormDataPart("shopId", bean.getShopId() + "")
                .setType(MultipartBody.FORM)
                .build();
        Request request = new Request.Builder()
                .url(NetAdress.insertAct)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }

    public Response selectOutPage(String id) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("id", id)
                .build();
        Request request = new Request.Builder()
                .url(NetAdress.selectOutPageByShop)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }

    public Response insertOutPage(OutPage.DataBean bean) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("name", bean.getName())
                .addFormDataPart("shopId", bean.getShopId()+"")
                .addFormDataPart("content", bean.getContent())
                .build();
        Request request = new Request.Builder()
                .url(NetAdress.insertOutPage)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }

    public Response insertAccept(String shopId,String userId,String id) throws IOException {
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("shopId",shopId)
                .addFormDataPart("pageId", id)
                .addFormDataPart("userId", userId)
                .build();
        Request request = new Request.Builder()
                .url(NetAdress.insertAccept)
                .header("User-Agent", "OkHttp Headers.java")
                .addHeader("Accept", "application/json; q=0.5")
                .post(requestBody)
                .build();
        return client.newCall(request).execute();
    }
}
